package com.guyan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guyan.entity.Cart;
import com.guyan.mapper.CartMapper;
import com.guyan.service.CartService;
import com.guyan.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * ClassName： CartServiceImpl
 * Date： 2021/6/14 15:17
 *
 * @author 孤焰
 * @version 1.0
 **/

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;

    @Override
    public PageInfo cartList(Integer page, Integer limit, Integer userId) {
        PageHelper.startPage(page, limit);
        PageInfo pageInfo = new PageInfo(cartMapper.cartList(userId));
        return pageInfo;
    }

    @Override
    public ResultVo updateCartById(Cart cart) {
        cart.setUpdateTime(new Date());
        return cartMapper.updateCartById(cart) > 0 ?
                ResultVo.success() : ResultVo.error(6000, cart+"数据库层更新出错");
    }

    @Override
    public ResultVo addCart(Cart cart) {
        Date date = new Date();
        cart.setCreateTime(date);
        cart.setCreateTime(date);

        return cartMapper.addCart(cart) > 0 ?
                ResultVo.success() : ResultVo.error(6000, cart+"数据库层添加失败");
    }

    @Override
    public ResultVo batchDelCart(Integer[] ids) {
        return cartMapper.batchDelCart(ids) > 0 ?
                ResultVo.success() : ResultVo.error(6000, "数据批量删除失败");
    }

    @Override
    public Cart getCartById(Integer id) {
        return cartMapper.getCartById(id);
    }
}
